#include "MainMenuState.h"
#include "Game.h"

#include "CreditsMenuState.h"
#include "PlayMenuState.h"
#include "OptionsMenuState.h"
#include "ProfileMenuState.h"
#include "BitmapFont.h"

MainMenuState* MainMenuState::pInstance = nullptr;
/*static*/ MainMenuState* MainMenuState::GetInstance( void )
{
	// Allocate the static pointer if necessary
	if( pInstance == nullptr )
		pInstance = new MainMenuState;

	// Return the singleton
	return pInstance;
}
/*static*/ void MainMenuState::DeleteInstance( void )
{
	delete pInstance;
	pInstance = nullptr;
}

// Update
void MainMenuState::Update(float deltaTime)
{
	if(controllerDelay > 0.0f)
		controllerDelay -= deltaTime;
	if(dI->KeyPressed(DIK_ESCAPE))
	{
		//close game
		Game::GetInstance()->Shutdown();

	}

	if(dI->KeyPressed(DIK_RETURN) || dI->JoystickButtonPressed(0))
	{
		//Switch states based upon current option selected
		switch(cursor)
		{
		case 0:
			Game::GetInstance()->ChangeState(PlayMenuState::GetInstance());
			break;
		case 1:
			Game::GetInstance()->ChangeState(ProfileMenuState::GetInstance());
			break;
		case 2:
			Game::GetInstance()->ChangeState(OptionsMenuState::GetInstance());
			break;
		case 3:
			Game::GetInstance()->ChangeState(CreditsMenuState::GetInstance());
			break;
		case 4:
			Game::GetInstance()->Shutdown();
			break;
		}
		return;
	}

	//Move cursor based upon input
	if(dI->KeyPressed(DIK_UP) || dI->KeyPressed(DIK_W) || dI->JoystickDPadPressed(DIR_UP) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.17f;
		if(cursor > 0)
			--cursor;
		else
			cursor = 4;
	}
	else if (dI->KeyPressed(DIK_DOWN) || dI->KeyPressed(DIK_S) || dI->JoystickDPadPressed(DIR_DOWN) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursor < 4)
			++cursor;
		else 
			cursor =0;
	}
}

// Render
void MainMenuState::Render(void) const
{
	//Render the Background
	if( BackGroundImage != -1 )
	{
		tM->Draw(BackGroundImage, 0,0,0.95f,1.055f);
	}

	//Get the draw position based on screen width and height
	int ScreenWidth = Game::GetInstance()->GetScreenWidth();
	int ScreenHeight = Game::GetInstance()->GetScreenHeight();

	//Cal Draw Pos
	int DrawPosX = ScreenWidth / 2 - 256;
	int DrawPosY = ScreenHeight / 2 - 256;

	//Render the blue blue
	tM->Draw(NavBlur, DrawPosX, DrawPosY);

	//Render the menu depends on the cursor
	switch(cursor)
	{
	case 0:
		{
			//Play Selected
			if( PlayButton != -1 )
				tM->Draw(PlayButton,DrawPosX, DrawPosY);
			break;
		}
	case 1:
		{
			//Profile Selected
			if( ProfileButton != -1 )
				tM->Draw(ProfileButton,DrawPosX, DrawPosY);
			break;
		}
	case 2:
		{
			//Option Selected
			if( OptionsButton != -1 )
				tM->Draw(OptionsButton,DrawPosX, DrawPosY);
			break;
		}
	case 3:
		{
			//Credits Selected
			if( CreditsButton != -1 )
				tM->Draw(CreditsButton,DrawPosX, DrawPosY);
			break;
		}
	case 4:
		{
			//Exit Selected
			if( ExitButton != -1 )
				tM->Draw(ExitButton,DrawPosX, DrawPosY);
			break;
		}
	}
	////Render all of the rectangles and text for each option
	//for(int i =0; i < 5; ++i)
	//{
	//	rect.top	 = 60 + 60*i + 10*i;
	//	rect.bottom	 = 120 + 60*i + 10*i;
	//	rect.left	 = 300; rect.right = 500;

	//	D3D->DrawRect(rect,D3DCOLOR_ARGB(255,255,0,0));
	//	switch(i)
	//	{
	//	case 0:
	//		//D3D->DrawText(_T("Play"), rect.left, rect.top , D3DCOLOR_ARGB(255,0,0,0));
	//		BitmapFont::GetInstance()->Print("Play", (float)rect.left, (float)rect.top,0, D3DCOLOR_ARGB(255,0,0,0));
	//		break;
	//	case 1:
	//		//D3D->DrawText(_T("Profile"), rect.left, rect.top , D3DCOLOR_ARGB(255,0,0,0));
	//		BitmapFont::GetInstance()->Print("Profile", (float)rect.left, (float)rect.top ,0, D3DCOLOR_ARGB(255,0,0,0));
	//		break;
	//	case 2:
	//		BitmapFont::GetInstance()->Print("Options", (float)rect.left, (float)rect.top ,0, D3DCOLOR_ARGB(255,0,0,0));
	//		break;
	//	case 3:
	//		BitmapFont::GetInstance()->Print("Credits", (float)rect.left, (float)rect.top ,0, D3DCOLOR_ARGB(255,0,0,0));
	//		break;
	//	case 4:
	//		BitmapFont::GetInstance()->Print("Exit", (float)rect.left, (float)rect.top ,0, D3DCOLOR_ARGB(255,0,0,0));
	//		break;
	//	}
	//}

	//RECT selectedRect;
	//selectedRect.top = 60 + 60*cursor + 10*cursor;
	//selectedRect.bottom = 120 + 60*cursor + 10*cursor;
	//selectedRect.left = 300; selectedRect.right = 500;
	//D3D->DrawHollowRect(selectedRect,D3DCOLOR_ARGB(255,255,255,0),3);
}

// Enter
//	- Used to initialize the game state.
void MainMenuState::Enter(void)
{
	GameState::Enter();

	BackGroundImage = tM->LoadTexture("Textures\\Menu\\MainMenuImage.jpg");
	PlayButton = tM->LoadTexture("Textures\\Menu\\MainMenu_SelPlay.png");
	OptionsButton = tM->LoadTexture("Textures\\Menu\\MainMenu_SelOptions.png");
	ProfileButton = tM->LoadTexture("Textures\\Menu\\MainMenu_SelProfile.png");
	ExitButton = tM->LoadTexture("Textures\\Menu\\MainMenu_SelExit.png");
	CreditsButton = tM->LoadTexture("Textures\\Menu\\MainMenu_SelCredits.png");
	NavBlur = tM->LoadTexture("Textures\\Menu\\Nav_Blur.png");
}

// Exit
//	- Used to clean up the game state.
void MainMenuState::Exit(void)
{
	GameState::Exit();
	DeleteInstance();
}


MainMenuState::MainMenuState(void)
{
	cursor = 0;

	BackGroundImage		 = -1;
	PlayButton			 = -1;
	ExitButton			 = -1;
	CreditsButton		 = -1;
	OptionsButton		 = -1;
	ProfileButton		 = -1;
	NavBlur				 = -1;
}
MainMenuState::~MainMenuState(void)
{
}